<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>发布订阅</title>
</head>
<body>
    
    <script>
        
        // 发布订阅是一对多  由多个监控一个   

        // 

        function Girl () {
            this._events = {};
            
        }

        Girl.prototype.on = function (eventName,callback){
            
            if(this._events[eventName]){
                this._events[eventName].push(callback);
            }else {
                this._events[eventName] = [callback];
            }
        };

        Girl.prototype.emit = function (eventName,...args){
            if(this._events[eventName]){
                this._events[eventName].forEach(ele=>ele(...args));
            }
        };

        let girl = new Girl();

        let cry = (who)=>{console.log(who + ' cry')};
        let eat = (who)=>{console.log(who + ' eat')};
        let shopping = (who)=>{console.log(who + ' shopping')};

        girl.on('lostLove',cry);
        girl.on('lostLove',eat);
        girl.on('lostLove',shopping);

        girl.emit("lostLove","You");

    </script>
</body>
</html>